<?php
/**
 *积分商城
 * wsl
 * 2022/7/16
 */
namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Http\Service\Integral;
use App\Repository\RepositoryInterface\IntegralInterface;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class IntegralController extends Controller
{
    //
    protected $integral;

    public function __construct(IntegralInterface $integral){
        $this->integral = $integral;
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 查询用户剩余积分
     */
    public function userRemainderIntegral(Request $request)
    {
        try {
            $uid = $request->input('user_id');
            //用户剩余积分
            $user_integral_balance = Integral::userRemainderIntegral($uid);
            return $this->success('success',['data' => $user_integral_balance]);
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @return void
     * 积分兑换商品所需积分获取
     */
    public function exchangeGoods()
    {
        try {
            //获取可兑换商品
            $is_integral_goods = $this->integral->isIntegral();
            $goods_integral = Integral::consumeIntegral($is_integral_goods);
            return ['code' => 20000 , 'msg' => 'success' , 'data' => $goods_integral];
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }


    /**
     * @return array
     * 积分兑换优惠券所需积分获取
     */
    public function exchangeCoupon()
    {
        try {
            //获取可兑换优惠券
            $is_integral_coupon = $this->integral->integralCoupon();
            return $is_integral_coupon;
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }


    /**
     * @param Request $request
     * @return string
     * 积分兑换物品
     */
    public function integralExchangeGoods(Request $request)
    {
        try {
            $goods_id = $request->input('goods_id');
            $user_id = $request->input('user_id');
            //商品所需积分
            $integral_exchange_goods = $this->integral->integralExchangeGoods($goods_id);
            //用户剩余积分
            $user_integral_balance = $this->integral->userIntegralBalance($user_id);
            //积分兑换
            $result = Integral::userIntegralBalance($user_id,$integral_exchange_goods,$user_integral_balance);
            return $result;
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }


    /**
     * @param Request $request
     * @return string
     * 积分流动记录查询
     */
    public function integralRecordingQuery(Request $request)
    {
        try {
            $uid = $request->input('user_id');
            $integral_recording_data = $this->integral->integralRecordingQuery($uid);
            return ['code' => 20000 , 'msg' => 'success' , 'data' => $integral_recording_data];
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }


    /**
     * @param Request $request
     * @return string
     * 积分流动记录删除
     */
    public function integralRecordingDelete(Request $request)
    {
        try {
            $Recording_id = $request->input('Recording_id');
            return $this->integral->integralRecordingDelete($Recording_id);
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }
}
